import { CollisionTag, DEL_BULLET_TIME, GetBuffType} from "../../config/play_def";
import { BoidStatus, BoidSkill, BoidArmor, BoidBuff } from "../../config/boid_def";
import { RoleSkill } from "../../config/role_def";
cc.Class({
    extends: cc.Component,

    properties: {
        orderId:0,
        first_collision:false,      // 第一次碰撞
        collision_destory:true,     // 碰撞及毁灭
        img_node:cc.Node,
        img_sprite:cc.Sprite,       // 攻击属性
        collision:cc.PolygonCollider,     // 碰撞组件
        attack_fly:false,           // 可攻击空中单位
        model:sp.Skeleton,          // 使用模型
        speed:1,                    // 速度倍数
        _damege:0,                  // 初始伤害
        damege:0,                   // 伤害
        move:false,                 // 移动开关
        script_name:'',             // 脚本名称    
        dir:cc.Vec2.ZERO,           // 飞行坐标
        count:0,                    // 帧数记录 一般不会大于一定帧数     
        overtime:5,               // 删除时间            
        from:'',                    // 炮弹来源
    },
    initInfo(info){
        // 组件赋值
        this.initComponent(info)
        // 组件赋值
        // 穿透
        this.from = info.attack_from
        if(info.through){
            this.collision_destory = false
        }else{
            this.collision_destory = true
        }
        if(info.overtime >= 0){
            this.overtime = info.overtime
        }else{
            this.overtime = DEL_BULLET_TIME
        }
        this.collider_boid_list = []
        this.effect = info.effect
        this.speed = info.speed
        this.damege = info.damege
        this._damege = info.damege
        this.dir = info.dir
        this.style = info.style

        this.voice = info.voice
        // 开始
        this.turnDir()
        this.move = true
    },
    clearInfo(){
        this.collider_boid_list = []
        this.effect = []
        this.speed = 1      // 速度归位
        this._damege = 0    // 伤害归位
        this.damege = 0     // 伤害归位
        this.count = 0      // 帧数归零
        this.move = false   // 关闭移动
        this.order_line = null  
        this.first_collision = false    // 是否初次碰撞
        this.dir = cc.Vec2.ZERO
        this.collider.enabled = false

        this.model.skeletonData = null
        this.model.setCompleteListener(null)
        gg.bulletManager.delBullet(this.orderId)
    },
    // 初始化组件
    initComponent(info){
        // 模型基础赋值
        this.model = this.node.getComponentInChildren(sp.Skeleton)
        let bulletSpine = he.load.get(info.style.url,info.style.type)
        this.model.skeletonData = bulletSpine
        if(info.style && info.style.skin){
            this.model.defaultSkin = info.style.skin
            this.model.setSkin(info.style.skin)
        }
        
        this.playAttack()
        this.model.setCompleteListener((trackEntry) => {
            var animationName = trackEntry.animation ? trackEntry.animation.name : "";
            this.listenAniTag(animationName)
        });

        // 碰撞重置
        this.collider = this.node.getComponent(cc.PolygonCollider)
        if(info.collider){
            let collider_str_list = info.collider.split('#')
            let collider_list = []
            for(let i = 0; i < collider_str_list.length; i++){
                let collider_str = collider_str_list[i].split('_')
                collider_list.push(cc.v2(parseInt(collider_str[0]),parseInt(collider_str[1])))
            }
            this.collider.points = collider_list
            this.collider.enabled = true
        }else{
            this.collider.enabled = false
        }
    },
    // 碰撞
    onCollisionEnter: function (other, self) {
        switch(other.tag){
            case CollisionTag.BOSS:
            case CollisionTag.MONSTER:
                if(this.first_collision){
                    return
                }
                let boid
                switch(other.tag){
                    case CollisionTag.MONSTER:
                        boid = other.node.getComponent('monster')
                    break
                    case CollisionTag.BOSS:
                        boid = other.node.getComponent('boss')
                    break
                }
                if(this.collider_boid_list.indexOf(boid.orderId) >= boid.orderId){
                    return
                }
                if(!boid.canAttack()){
                    return
                }
                if(boid.miss){
                  let randomRes = Math.random() * 100
                  if(boid.miss > randomRes){
                    boid.missAni()
                    this.clearInfo()
                    return
                  }
                }
                this.collider_boid_list.push(boid.orderId)
                this.attack(boid)
            break
        }
    },
    // 攻击 伤害转化
    attack(monster){},
    // 攻击效果
    happenEffect(boid,damege){
        if(!damege){
            damege = this.damege
        }
        if(!boid.canAttack()){
            return false
        }
        if(boid.fly && !this.attack_fly){
            return false
        }
        if(this.effect == null){
            return false
        }
        if(this.script_name != `parabola_bullet` && this.voice){
            he.voice.playEffect(`${this.voice}`,`voice`)
        }
        // 技能
        let cirt_damege = 0
        if(this.effect && this.effect.length && this.effect != null){
            for(let i = 0;i < this.effect.length;i++){
                let effect = this.effect[i]
                // 伤害效果
                switch(effect.key){
                    case RoleSkill.BoidSLow: // 减速
                        let slowRes
                        slowRes = effect.value
                        let slowBuff = {
                            uid: effect.uid,
                            key : BoidBuff.Slow,
                            value:slowRes,
                            time : parseInt(effect.time) 
                        } 
                        boid.addBuff(slowBuff)
                        boid.hpHurt(this.from,this.damege)
                    break
                    case RoleSkill.Repel: // 击退,Boss免疫击退
                        if(boid.rank == 5){
                            boid.hitBack(0,effect.repel)
                        }
                        else{
                            boid.hitBack(effect.value,effect.repel)
                        }
                        boid.hpHurt(this.from,damege)
                    break
                    case RoleSkill.DOT: // 持续性伤害
                        let DOTInfo = boid.getBuffInfo(effect.uid,GetBuffType.KAU,BoidBuff.DOT)
                        let DOTres
                        let dotTime = effect.time
                        if(DOTInfo){
                            dotTime = DOTInfo.time
                            DOTres = DOTInfo.count + 1
                            if(DOTres > DOTInfo.max){
                                DOTres = DOTInfo.max
                            }
                        }else{
                            DOTres = 1
                        }
                        let DOTbuff = {
                            uid: effect.uid,
                            key : BoidBuff.DOT,
                            value:effect.value,
                            count:DOTres,
                            max: effect.max,
                            time : dotTime,
                            interval: effect.time,
                            overtime: effect.overtime,
                            from:this.from,
                            ani:effect.ani
                        } 
                        boid.addBuff(DOTbuff)
                        boid.hpHurt(this.from,this.damege)
                    break
                    case RoleSkill.Crit: // 持续性伤害
                        let critRes = Math.random() * 100
                        if(critRes <= effect.pro){
                            let new_crit_damege = parseInt(damege * effect.mul)
                            if(new_crit_damege > cirt_damege){
                                cirt_damege = new_crit_damege
                            }                            
                        }
                    break
                    // case RoleSkill.AOE:
                    //     let aoeRes = Math.random() * 100
                    //     boid.hpHurt(this.from,damege)
                    //     damege = parseInt(damege + damege * (effect.value/100))
                    //     if(aoeRes <= effect.pro){
                    //         let currentPos = boid.getWorldPos()
                    //         for(let i = gg.boidsManager.boids_list.length - 1;i >= 0;i--){
                    //             if(gg.boidsManager.boids_list[i].orderId == boid.orderId){
                    //                 continue
                    //             }
                    //             if(!gg.boidsManager.boids_list[i].canAttack()){
                    //                 continue
                    //             }
                    //             let boidsWorldPos = gg.boidsManager.boids_list[i].getWorldPos()
                    //             let scale = gg.boidsManager.boids_list[i].node.scale
                    //             let distance = currentPos.sub(boidsWorldPos).mag() * scale
                    //             let plash = effect.range * scale
                    //             if(distance < plash){
                    //                 if(effect.ani){
                    //                     gg.boidsManager.boids_list[i].addBuffAni(effect.ani)
                    //                 }
                    //                 // 播放动画
                    //                 gg.boidsManager.boids_list[i].hpHurt(this.from,damege)
                    //             }
                    //         }
                    //     }
                    // break
                    // case RoleSkill.Splash:
                    //     let splashRes = Math.random() * 100
                    //     boid.hpHurt(this.from,damege)
                    //     damege = parseInt(damege + damege * (effect.value/100))
                    //     if(splashRes <= effect.pro){
                    //         let currentPos = boid.getWorldPos()
                    //         for(let i = gg.boidsManager.boids_list.length - 1;i >= 0;i--){
                    //             if(gg.boidsManager.boids_list[i].orderId == boid.orderId){
                    //                 continue
                    //             }
                    //             if(!gg.boidsManager.boids_list[i].canAttack()){
                    //                 continue
                    //             }                                
                    //             let boidsWorldPos = gg.boidsManager.boids_list[i].getWorldPos()
                    //             if(currentPos.y + 10 > boidsWorldPos.y){
                    //                 continue
                    //             }
                    //             let scale = gg.boidsManager.boids_list[i].node.scale
                    //             let distance = (currentPos.sub(boidsWorldPos).mag() - boid.node.height) * scale
                    //             let plash = effect.range * scale
                    //             if(distance < plash){
                    //                 if(effect.ani){
                    //                     gg.boidsManager.boids_list[i].addBuffAni(effect.ani)
                    //                 }
                    //                 // 播放动画
                    //                 gg.boidsManager.boids_list[i].hpHurt(this.from,damege)
                    //             }
                    //         }
                    //     }
                    //     break
                    // case RoleSkill.Stun:
                    //     let stunRes = Math.random() * 100
                    //     if(stunRes <= effect.pro){
                    //         let stunBuff = {
                    //             key : BoidBuff.Stun,
                    //             value : 1,
                    //             time : parseInt(effect.value)
                    //         }
                    //         boid.addBuff(stunBuff)
                    //     }
                    //     boid.hpHurt(this.from,this.damege)
                    // break
                    // default:
                    //     boid.hpHurt(this.from,damege)
                    // break                    
                }
                if(cirt_damege){
                    boid.addBuffAni('spine/ani/critical_strike','default')
                    boid.hpHurt(this.from,cirt_damege)
                }else{
                    boid.hpHurt(this.from,damege)
                }
                // 后置特效
                if(effect.prefab){
                    let param = JSON.parse(JSON.stringify(effect))
                    param.pos = boid.getWorldPos().sub(cc.v2(0,100))
                    if(param.top){
                        bulletManager.addTopEffect(param)
                    }else{
                        bulletManager.addEffect(param)
                    }
                }
            }
            return true
        }else{
            boid.hpHurt(this.from,damege)
            return true
        }
    },
    // 动画监听
    listenAniTag(tag){ 
        let breakS = tag.indexOf('_')
        let aniType = tag
        if(breakS >= 0){
            aniType = tag.substring(0,breakS)
        }
        switch (aniType) {
            case 'hit':
                this.clearInfo()
            break
        }
    },
    // 受击动画
    playHit(boid){
        // this.clearInfo()
        if(this.collision_destory){
            this.model.setAnimation(0,'hit',false)
        }else{
            if(this.style){
                boid.addBuffAni(this.style.url,this.style.skin)
            }
        }
    },
    // 攻击动画
    playAttack(){
        this.model.defaultAnimation = 'attack'
        this.model.setAnimation(0,'attack',true)
    },
    fly(dt){
    //   if(this.node.y > 0){
    //     this.clearInfo()
    //   }
    },
    turnDir(){},  // 转向函数
    update (dt) {
        if(this.move  && !gg.data.pause && gg.data.status){
            this.fly(dt)
            if(this.overtime > 0){
                this.count += dt
                if(this.count >= this.overtime){
                    this.clearInfo()
                }
            }
        }
    },
});
